home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample CSAM / Sources / AOCE•Util.C next >
Encoding:
Text File  |  1995-07-28  |  6.4 KB  |  198 lines  |  [TEXT/MPS ]

  1. // Copyright (c) 1993 Regents of the University of Michigan. All rights reserved.
  2.  
  3. /* ==========================================================================
  4.     AOCE utility routines for the ldap CSAM.
  5.  
  6.     Written by: Gavin Eadie • The University of Michigan Information Systems
  7.           Phone: (313) 936-0816
  8.      AppleLink: A67
  9.       Internet: gavin@umich.edu
  10.  
  11.     ==========================================================================    */
  12.  
  13. #include "SKEL•CSAM.h"
  14. #include "AOCE•Util.h"
  15.  
  16. #include <string.h>            // strxxx + memxxx routines
  17.  
  18. pascal Boolean AttrValue_Callback(long clientData, const Attribute *attribute);
  19. void MakePersonalRecordID(RecordID * recordID, const CreationID * creationID);
  20.  
  21. #define kAttrBuffer_Len 256
  22.  
  23. typedef struct AttributeCopyInfo {
  24.     Ptr        buffer_Ptr;
  25.     Size        buffer_Len;
  26.     Boolean    AttrValue_OK;
  27. } AttributeCopyInfo;
  28.  
  29. /* ==========================================================================
  30.         GetAttributeValue • 
  31.  
  32.             this routine returns a single attribute value from a record.  it's
  33.             used to get information out of the directory setup information
  34.     --------------------------------------------------------------------------    */
  35. OSErr GetAttributeValue(const CreationID * cid, short dsRef,
  36.                                 AttributeType * attribType, void * attrBuffer,
  37.                                 Size attrBufferSize) {
  38.     DirParamBlock        cat_pb;
  39.     Ptr                     AttrBuffer_Ptr;
  40.     RecordIDPtr         recordList[1];
  41.     AttributeTypePtr    attrList[1];
  42.     RecordID             recordID;
  43.     AttributeCopyInfo    callbackInfo;
  44.     OSErr                 Dir_Error;
  45.     
  46.     callbackInfo.buffer_Ptr = attrBuffer;
  47.     callbackInfo.buffer_Len = attrBufferSize;
  48.     callbackInfo.AttrValue_OK = false;
  49.     
  50.     MakePersonalRecordID(&recordID, cid);
  51.     
  52.     recordList[0] = (RecordIDPtr)&recordID;
  53.  
  54.     attrList[0] = attribType;
  55.     AttrBuffer_Ptr = NewPtr(kAttrBuffer_Len);
  56.     if (noErr != MemError())
  57.         return MemError();
  58.     
  59.     memset(&cat_pb, '\0', sizeof(DirParamBlock));
  60.         
  61.     cat_pb.header.ioCompletion = nil;
  62.     cat_pb.header.clientData = (long)&callbackInfo;
  63.     cat_pb.lookupGetPB.serverHint.aNet = 0;
  64.     cat_pb.lookupGetPB.serverHint.aNode = 0;
  65.     cat_pb.lookupGetPB.serverHint.aSocket = 0;
  66.     cat_pb.lookupGetPB.dsRefNum = dsRef;
  67.     cat_pb.lookupGetPB.identity = 0;
  68.     cat_pb.lookupGetPB.aRecordList = recordList;
  69.     cat_pb.lookupGetPB.attrTypeList = attrList;
  70.     cat_pb.lookupGetPB.recordIDCount = 1;
  71.     cat_pb.lookupGetPB.attrTypeCount = 1;
  72.     cat_pb.lookupGetPB.includeStartingPoint = true;
  73.     cat_pb.lookupGetPB.getBuffer = AttrBuffer_Ptr;
  74.     cat_pb.lookupGetPB.getBufferSize = kAttrBuffer_Len;
  75.     cat_pb.lookupGetPB.startingRecordIndex = 1;
  76.     cat_pb.lookupGetPB.startingAttrTypeIndex = 1;
  77.  
  78.     OCESetCreationIDtoNull(&cat_pb.lookupGetPB.startingAttribute.cid);
  79.     
  80.     Dir_Error = DirLookupGet(&cat_pb, false);
  81.     if (noErr == Dir_Error || kOCEMoreData == Dir_Error) {
  82.         cat_pb.lookupParsePB.eachRecordID = nil;
  83.         cat_pb.lookupParsePB.eachAttrType = nil;
  84.         cat_pb.lookupParsePB.eachAttrValue = AttrValue_Callback;
  85.         Dir_Error = DirLookupParse(&cat_pb, false);
  86.     }
  87.     
  88.     if ((noErr == Dir_Error) && !callbackInfo.AttrValue_OK)
  89.         Dir_Error = kNoRecords;
  90.         
  91.     DisposePtr(AttrBuffer_Ptr);
  92.  
  93.     return (Dir_Error);
  94. }
  95.  
  96.  
  97. /* ==========================================================================
  98.         AttrValue_Callback • 
  99.  
  100.     --------------------------------------------------------------------------    */
  101. pascal Boolean AttrValue_Callback(long clientData, const Attribute * attribute) {
  102.     AttributeCopyInfo *    callbackInfo;
  103.     Size                         moveSize;
  104.     
  105.     callbackInfo = (AttributeCopyInfo *)clientData;
  106.     
  107.     moveSize = attribute->value.dataLength;
  108.     if (moveSize > callbackInfo->buffer_Len)
  109.         moveSize = callbackInfo->buffer_Len;
  110.  
  111.     BlockMove(attribute->value.bytes, callbackInfo->buffer_Ptr, moveSize);
  112.  
  113.     callbackInfo->AttrValue_OK = true;
  114.     return (false);
  115. }
  116.  
  117.  
  118. /* ==========================================================================
  119.         AddAttribute • add an attribute value to a record
  120.  
  121.     --------------------------------------------------------------------------    */
  122. OSErr AddAttribute(const CreationID * cid, short dsRef,
  123.                          AttributeType * attribType, void * data,
  124.                          unsigned long dataLength, AttributeTag tag) {
  125.     DirParamBlock    cat_pb;
  126.     Attribute         att_pb;
  127.     RecordID         recordID;
  128.     OSErr             Dir_Error;
  129.     
  130.     MakePersonalRecordID(&recordID, cid);
  131.     
  132.     OCECopyRString((const RStringPtr)attribType, (RStringPtr)&att_pb.attributeType, kAttributeTypeMaxBytes);
  133.     OCESetCreationIDtoNull(&att_pb.cid);
  134.     att_pb.value.tag = tag;
  135.     att_pb.value.dataLength = dataLength;
  136.     att_pb.value.bytes = (Ptr)data;
  137.  
  138.     cat_pb.addAttributeValuePB.serverHint.aNet = 0;
  139.     cat_pb.addAttributeValuePB.serverHint.aNode = 0;
  140.     cat_pb.addAttributeValuePB.serverHint.aSocket = 0;
  141.     cat_pb.addAttributeValuePB.dsRefNum = dsRef;
  142.     cat_pb.addAttributeValuePB.identity = 0;
  143.     cat_pb.addAttributeValuePB.aRecord = &recordID;
  144.     cat_pb.addAttributeValuePB.attr = &att_pb;
  145.  
  146.     Dir_Error = DirAddAttributeValue(&cat_pb, false);
  147.  
  148.     return (Dir_Error);
  149. }
  150.  
  151. /* ==========================================================================
  152.         MakePersonalRecordID •
  153.         
  154.             this convenience function fills in a RecordID given a creation ID,
  155.             since a fully specified record ID for a personal directory contains
  156.             only a creation ID
  157.     --------------------------------------------------------------------------    */
  158. void MakePersonalRecordID(RecordID * recordID, const CreationID * creationID) {
  159.     recordID->rli = nil;
  160.     OCECopyCreationID(creationID, &recordID->local.cid);
  161.     recordID->local.recordName = nil;
  162.     recordID->local.recordType = nil;
  163. }
  164.  
  165.  
  166. //    --------------------------------------------------------------------------
  167. //        copy an RStr to a given CStr and return the address of the CStr ...
  168. //
  169. //                                    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  170. //                            RStr:    |   |(n)|a b c d e f g ...
  171. //                                    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- -
  172. //                            CStr:    |a|b|c|d|e|f|g|h| | | 
  173. //                                    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- -
  174. //                                    [0]
  175. //    --------------------------------------------------------------------------
  176. char * RToCString(RStringPtr R_String, char * C_String) {
  177.  
  178.     BlockMove(R_String->body, C_String, R_String->dataLength);
  179.     C_String[R_String->dataLength] = 0;
  180.     return (C_String);
  181.  
  182. }
  183.  
  184.  
  185. //    --------------------------------------------------------------------------
  186. //        look for the Target character in an RStr and return true if found
  187. //    --------------------------------------------------------------------------
  188. Boolean rstrchr(RStringPtr R_String, short Target) {
  189.     char *    RStr_Body = R_String->body;
  190.     short        RStr_Index = 0;
  191.  
  192.     while (RStr_Index <= R_String->dataLength) {
  193.         if (Target == RStr_Body[RStr_Index++]) return (true);
  194.     }
  195.  
  196.     return (false);
  197. }
  198.